import CryptoJS from 'crypto-js'

/**
 * AES ECB模式加密
 * @param password 要加密的字符串
 * @param key 密钥
 */
export function aesEncryptECB(password: string, key: string) {
  if (!password || !key) {
    return
  }
  const passwordHex = CryptoJS.enc.Utf8.parse(password)
  const keyHex = CryptoJS.enc.Utf8.parse(key)

  const options = {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
  }
  const encryptPassword = CryptoJS.AES.encrypt(passwordHex, keyHex, options).toString()
  //   console.log('加密后', encryptPassword)
  return encryptPassword
}

/**
 * AES ECB模式解密
 * @param encryptPassword 经过AES加密后的字符串
 * @param key 密钥
 */
export function aesDecryptECB(encryptPassword: string, key: string) {
  if (!encryptPassword || !key) {
    return
  }
  const keyHex = CryptoJS.enc.Utf8.parse(key)

  const options = {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
  }

  const bytes = CryptoJS.AES.decrypt(encryptPassword, keyHex, options)
  const originalText = bytes.toString(CryptoJS.enc.Utf8)
  //   console.log('解密后', originalText)

  return originalText
}
